﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace DYSales.Views.sys
{
    public partial class cwin_updatePwd : ChildWindow
    {
        public cwin_updatePwd()
        {
            InitializeComponent();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (tb_旧操作员密码.Password != string.Empty && tb_新操作员密码.Password != string.Empty)
            {
                OKButton.IsEnabled = false;
                dm.getDS().Load(dm.getDS().GetOperatorsQuery(), lo =>
                {
                    if (!lo.HasError)
                    {
                        var user = dm.getDS().Operators.FirstOrDefault(o => o.ot_用户名.Equals(WebContext.Current.User.Name));
                        if (user != null)
                        {
                            if (user.ot_密码.Equals(tb_旧操作员密码.Password))
                            {
                                user.ot_密码 = tb_新操作员密码.Password;
                                dm.getDS().SubmitChanges(le =>
                                {
                                    if (!le.HasError)
                                    {
                                        this.DialogResult = true;
                                    }
                                }, null);
                            }
                            else
                            {
                                OKButton.IsEnabled = true;
                                tbk_msg.Text = "旧密码不正确，修改密码失败";
                            }
                        }
                    }
                }, null);
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

